<template>
  <div class="dialog-container">
    <div class="dialog-content">
      <el-form ref="elForm" size="mini" label-width="0">
        <el-form-item>
          <el-checkbox v-model="checked">导出我方早发数据</el-checkbox>
        </el-form-item>
        <el-form-item style="margin-bottom: 0; margin-top: 40px">
          <div class="panel-line flex-end">
            <el-button @click="onCancel">取消</el-button>
            <el-button :loading="loading" type="primary" @click="onSubmit">确定</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { apiPmExport } from '@/api/biddingNotice'

export default {
  name: 'DialogExport',
  props: {
    time: Object,
  },
  data() {
    return {
      loading: false,
      checked: true,
    }
  },
  mounted() {
    // console.log(this.time)
  },
  destroyed() {},
  methods: {
    // 取消
    onCancel() {
      this.$emit('close')
    },
    // 确定
    onSubmit() {
      this.loading = true
      const info = {
        start_time: this.time.start_time,
        end_time: this.time.end_time,
        if_export_early: this.checked ? 1 : 2,
        label: '拍卖',
      }
      apiPmExport(info)
        .then((res) => {
          let blob = new Blob([res], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          })
          let url = window.URL.createObjectURL(blob)
          this.download(url, '竞品对比统计.xls')
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 下载文件
    download(blobUrl, name) {
      const a = document.createElement('a')
      a.style.displav = 'none'
      a.download = name
      a.href = blobUrl
      a.click()
      this.$message({
        message: '操作成功',
        type: 'success',
      })
      setTimeout(() => {
        this.loading = false
        this.$emit('close')
      }, 1000)
    },
  },
}
</script>
